package com.hqw.jzdemo.view;

import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.Event;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.fengfei.ffadsdk.AdViews.Splash.FFSpalshListener;
import com.hqw.jzdemo.BuildConfig;
import com.hqw.jzdemo.MainActivity;
import com.hqw.jzdemo.R;
import com.hqw.jzdemo.util.LogUtil;

//import com.facebook.react.views.webview.events.TopLoadingFinishEvent;

/**
 * ReactJzAdView
 *
 * Created by tiangui on 2020/01/13.
 */
public class ReactJzAdView extends LinearLayout {

    private static final String TAG = "ReactNative-jz";

    private ReactContext mContext;
    private RelativeLayout mContainer;

    private final Runnable measureAndLayout = new Runnable() {
        @Override
        public void run() {
            int w = MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY);
            int h = MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY);
            measure(w, h);
            layout(getLeft(), getTop(), getRight(), getBottom());
        }
    };

    public ReactJzAdView(ReactContext context, AttributeSet set) {
        super(context, set);

        mContext = context;

        LayoutInflater layoutInflater = context.getCurrentActivity().getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.view_react_jz_ad, this);
        mContainer = view.findViewById(R.id.container);

        initView();
    }

    @Override
    public void requestLayout() {
        super.requestLayout();
        post(measureAndLayout); // 解决 RN 动态 addView 页面不刷新的问题
    }

    private void initView() {
        LogUtil.i(TAG, "initView()");

        mContainer.addOnAttachStateChangeListener(new OnAttachStateChangeListener(){
            @Override
            public void onViewAttachedToWindow(View v) {
                LogUtil.i(TAG, "onViewAttachedToWindow()");
            }

            @Override
            public void onViewDetachedFromWindow(View v) {
                LogUtil.i(TAG, "onViewDetachedFromWindow()");
            }
        });

    }

    // TODO  可能需要 check mContext 的有效性
    public void requestAd() {
        MainActivity activity = (MainActivity) mContext.getCurrentActivity();
        activity.getFFSplashManager().requestAd(activity, BuildConfig.appId, BuildConfig.splashId, mContainer, new FFSpalshListener() {
            @Override
            public void onAdDisplay() {
                LogUtil.i(TAG, "onAdDisplay()");
                Toast.makeText(mContext, "广告加载成功", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onAdClose() {
                LogUtil.i(TAG, "onAdClose()");
            }

            @Override
            public void onAdRecieved() {
                LogUtil.i(TAG, "onAdRecieved()");
            }

            @Override
            public void onAdClick() {
                LogUtil.i(TAG, "onAdClick()");
            }

            @Override
            public void onAdFailed(String s) {
                LogUtil.i(TAG, "onAdFailed()");
            }

            @Override
            public void onAdSpreadNotifyCallback(String s, ViewGroup viewGroup, int i, int i1) {
                LogUtil.i(TAG, "onAdSpreadNotifyCallback()");
            }
        });

    }

    /**
     * copy from ReactWebViewManager.java
     */
    public static void emitFinishEvent(ReactJzAdView alignedTextView, int contentHeight) {
//        dispatchEvent(
//                alignedTextView,
//                new TopLoadingFinishEvent(
//                        alignedTextView.getId(),
//                        createWebViewEvent(alignedTextView, contentHeight)));
    }

    private static void dispatchEvent(ReactJzAdView alignedTextView, Event event) {
        ReactContext reactContext = (ReactContext) alignedTextView.getContext();
        EventDispatcher eventDispatcher =
                reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
        eventDispatcher.dispatchEvent(event);
    }

    private static WritableMap createWebViewEvent(ReactJzAdView alignedTextView, int contentHeight) {
        WritableMap event = Arguments.createMap();

        event.putInt("target", alignedTextView.getId());
        event.putDouble("contentHeight", contentHeight);

        return event;
    }

}
